<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	分销插件所有金额字段均保存整形，精确到分。显示时，除以100然后保留两位小数即可
	佣金比例字段保存整形，值为万分数，相当于百分数的精确到小数点后2位。即展示到前端时，需要除以100，换算成百分数显示
*/
class member extends \plugin{
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('会员注册失败，缺少会员ID！');
		}
		$this->insert('member',$member);
		//新人发券
		$coupon=$this->pdo_get('coupon_sendticket',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
		if($coupon){
			$params=array(
				'mid'=>$member['mid'],
				'ac_type'=>'new',
				'ac_id'=>$coupon['id'],
				'remark'=>'新人发券活动',
			);
			$this->model('coupon')->give_coupon($params);
		}
		//新人参与游戏
		$game=$this->pdo_get('game',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>3));
		if($game){
				$params=array(
					'mid'=>$member['mid'],
					'game_id'=>$game['id'],
					'remark'=>'购物参与活动',
				);
				$this->model('game')->add_game($params);
		}
	}
	public function checklogin($storeid='',$area_agentid='',$area_code=''){
		global $_AR;
		if(!$_SESSION['uid']){
			if($_SESSION['os']=='windows'){
				if($_SESSION['show_lang']=='en'){
					exi('Please login first！','error','jy_weishop/member.login/');
				}else{
					exi('请先登陆！','error','jy_weishop/member.login/');
				}
				
			}else{
				$_SESSION['forward_url']=$_AR['access_json'];
				model('core/core')->checklogin();
			}
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_weishop/member.index/');
			}else{
				$user=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'storeid'=>$storeid?:$_SESSION['storeid'],
						'area_agentid'=>$area_agentid?:$_SESSION['area_agentid'],
						'area_code'=>$area_code?:$_SESSION['area_code'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_weishop_member',$insert);
				}
			}
		}
	}
	public function get_level(){
		$levelid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'level');
		$leveltitle = pdo_getcolumn('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$levelid),'title');
		$info=array('id'=>$levelid,'title'=>$leveltitle);
		return $info;
	}
	public function get_group(){
		$groupid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'groupid');
		$grouptitle = pdo_getcolumn('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$groupid),'title');
		$info=array('id'=>$groupid,'title'=>$grouptitle);
		return $info;
	}
	/*
		会员等级变化，稍后完善
	*/
	public function get_next_level($mid,$isall=false){
		$current_level=$this->pdo_getcolumn('member',array('mid'=>$mid),'level');
		$member_level=$this->pdo_getcolumn('member_level',array('id'=>$current_level),'level');
		$params=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'isup'=>1,'level >'=>$member_level),
			'orderby'=>'level asc',
		);
		$member_levels=$this->pdo_getlist('member_level',$params,$total);		
		if(!$member_levels){
			return array();
		}
		if($isall){
			return $member_levels;
		}
		return $member_levels[0];
	}
	public function up_member_level($mid,$level_id){
		$this->pdo_update('member',array('level'=>$level_id),array('mid'=>$mid));
		return true;
	}
	public function check_uplevel($mid,$up_type=0){
		$mid=$mid?:$_SESSION['uid'];
		$member_levels=$this->model('member')->get_next_level($mid,true);
		//高等级匹配开始
		paixu($member_levels,'level','desc');
		foreach($member_levels as $item){
			if($item['id'] && $item['up_type']==$up_type){
				//升级会员等级
				$ordercount=$this->pdo_count('order',array('mid'=>$mid,'status'=>3),'id');
				$ordermoney=$this->pdo_getsum('order',array('mid'=>$mid,'status'=>3),'price');
				$ordermoney=$ordermoney?:0;
				if($item['goods_ids']){
					$goods_ids=json_decode($item['goods_ids'],true);
					$lastbuytime=time()-24*3600;
					foreach($goods_ids as $gv){
						$isgoods=true;
						$isbuy=$this->pdo_count('order_goods',array('mid'=>$mid,'goodsid'=>$gv['id'],'ispay'=>1,'createtime >'=>$lastbuytime),'id');
						if($isbuy>0){
							break;
						}
					}
				}
				if((!$isgoods || ($isgoods && $isbuy>0)) && $ordercount>=$item['ordercount'] && $ordermoney>=$item['ordermoney']){
					$this->up_member_level($mid,$item['id']);
				}
			}
			if($isbuy>0){
				break;
			}
		}
		return true;
	}
}